<template>
    <view>
        <!-- pages/auth/auth.wxml -->
        <button type="primary" plain open-type="getUserInfo" @getuserinfo="handleGetUserInfo">获取授权</button>
    </view>
</template>

<script>
// pages/auth/auth.js
import { login } from '../../utils/asyncWx.js';
export default {
    data() {
        return {};
    },
    methods: {
        async handleGetUserInfo(e) {
            try {
                const { encryptedData, rawData, iv, signature } = e.detail;
                const { code } = await login();
                console.log(code);
                const token =
                    'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo';
                // 写死的token，实际中应该用下面注释掉的从服务器获取
                // const token=await request({url:"/users/wxlogin",data:loginParams,method:"post"});
                uni.setStorageSync('token', token);
                uni.navigateBack({
                    delta: 1
                });
            } catch (error) {
                console.log('CatchClause', error);
                console.log('CatchClause', error);
                console.error('Login error:', error);
                uni.showToast({
                    title: '登录失败，请重试',
                    icon: 'none',
                    duration: 2000
                });
            }
        }
    }
};
</script>
<style>
/* pages/auth/auth.wxss */
</style>
